package org.example.mr2;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class Fruit2Driver implements Tool {

    private Configuration configuration;

    @Override
    public int run(String[] args) throws Exception {

        //1、获取job的对象
        Job job = Job.getInstance(configuration);
        //2、设置主类路径
        job.setJarByClass(Fruit2Driver.class);
        //3、设置Mapper输出KV类型·
        TableMapReduceUtil.initTableMapperJob("fruit",
                new Scan(),
                Fruit2Mapper.class,
                ImmutableBytesWritable.class,
                Put.class,
                job
        );
        //4、获取job的对象
        TableMapReduceUtil.initTableReducerJob("fruit2",
                Fruit2Reducer.class,
                job
        );

        //5、获取job的对象
        boolean b = job.waitForCompletion(true);
        return b ? 0 : 1;
        //6、获取job的对象
        //7、获取job的对象

    }

    @Override
    public void setConf(Configuration conf) {
        configuration = conf;
    }

    @Override
    public Configuration getConf() {
        return configuration;
    }


    public static void main(String[] args) {
        try {
//            Configuration configuration = new Configuration();
            Configuration configuration = HBaseConfiguration.create();
            ToolRunner.run(configuration, new Fruit2Driver(), args);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
